import Config from './config.js';
import Api from './api.js';
import Toast from '@vant/weapp/toast/toast';

function getToken() {
  return wx && wx.globalData && wx.globalData.token ? wx.globalData.token : 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJ0aGlzIGlzIExJTkZFTiB0b2tlbiIsImF1ZCI6IkxJTkZFTiIsImlzcyI6IkxJTkZFTiIsImV4cCI6MTU5MTQxMTk3NiwidXNlcklkIjo1MiwiaWF0IjoxNTkxNDA0Nzc2fQ.DNTeOtZdldlpJpSF3mY5E5Lfs5s-uj0bySdRvqTp6ZU';
}

export default {
  api: Api,
  post(url, data) {
    let header = {
      'Authorization': getToken(),
      'Content-Type':'application/json;charset=UTF-8',
			'Content-Type':'application/x-www-form-urlencoded'
    };

    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: data,
        header: header,
        timeout: Config.ajax.timeout,
        method: 'post',
        success: (res) => {
          if (res && res.data && res.data.code === 1) {
            if (res.data.result && res.data.result.token) {
              wx.globalData.token = res.data.result.token;
            }
            resolve(res.data.result)
          } else {
            Toast(res.data.msg ? `${res.data.msg}` : '服务端未知错误！')
            reject(res.data)
          }
        },
        fail: (res) => {
          Toast(res.errMsg);
          reject(res)
        },
        
      })
    })
  },

  get(url, data) {
    let header = {
      'Authorization': getToken(),
      'Content-Type':'application/json;charset=UTF-8',
			'Content-Type':'application/x-www-form-urlencoded'
    };
    return new Promise((resolve, reject) => {
      wx.request({
        url: url,
        data: data,
        header: header,
        timeout: Config.ajax.timeout,
        method: 'GET',
        success: (res) => {
          if (res && res.data && res.data.code === 1) {
            resolve(res.data.result)
          } else {
            Toast(res.data.msg ? `${res.data.msg}` : '服务端未知错误！')
            reject(res.data)
          }
        },
        fail: (res) => {
          Toast(res.errMsg);
          reject(res)
        },
        
      })
    })
  },

  upload(file, callback) {
    if (file && (file.path || file.url)) {
      wx.uploadFile({
        url: this.api.wx.fileUpload,
        filePath: file.path || file.url,
        timeout: 40 * 1000,
        name: 'file',
        formData: {
          filename: file.name || '',
          method: 'POST'
        },
        header: {
          'Content-Type': 'multipart/form-data',
          'Authorization': getToken()
        },
        success(res) {
          if (res && res.data) {
            callback && callback.success && callback.success(JSON.parse(res.data));
          } else {
            callback && callback.error && callback.error('未知的上传错误！', res);
          }
        },
        fail: (res) => {
          callback && callback.error && callback.error(res.errMsg, res);
        },
        complete: () => {
          callback && callback.finally && callback.finally();
        }
      });
    }
  }
};